﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class BrandManager:BusinessLogic
    {
        public BRANDS Load(int ID)
        {
            return (BRANDS)ObjectRow.Load(typeof(BRANDS), string.Format("this.ID={0}", ID));
        }

        public BRANDS LoadID(string BrandCode)
        {
            return (BRANDS)ObjectRow.Load(typeof(BRANDS), string.Format("this.BrandCode='{0}'", BrandCode));
        }

        public BRANDS LoadName(string BrandName,decimal DomainID)
        {
            return (BRANDS)ObjectRow.Load(typeof(BRANDS), string.Format("this.BrandName='{0}' AND this.DOMAINID='{1}'", BrandName, DomainID));
        }

        public BRANDS LoadWithDomainID(string BrandCode, decimal DomainID)
        {
            return (BRANDS)ObjectRow.Load(typeof(BRANDS), string.Format("this.BrandCode='{0}' AND this.DOMAINID='{1}'", BrandCode, DomainID));
        }

        public BRANDS Load(string code, decimal domainID, decimal ID)
        {
            return (BRANDS)ObjectRow.Load(typeof(BRANDS), string.Format("this.BrandCode='{0}' and this.DOMAINID={1} and this.ID<>'{2}'", code, domainID, ID));
        }

        //public static ObjectTable GetBrandsTable(string CategoryCode, string ManuCode,decimal domainid)
        //{
        //    StringBuilder sb = new StringBuilder();

        //    if (CategoryCode != null && CategoryCode.Length > 0)
        //    {
        //        sb.AppendFormat("this.CategoryCode='{0}'", CategoryCode);
        //    }
        //    if (ManuCode != null && ManuCode.Length > 0)
        //    {
        //        sb.AppendFormat("and this.ManuCode='{0}'", ManuCode);
        //    }
        //    if (domainid >0 )
        //    {
        //        sb.AppendFormat("and this.DOMAINID='{0}'", domainid);
        //    }

        //    return new ObjectTable(typeof(BRANDWITHCATEGORY), sb.ToString().TrimStart('a', 'n', 'd'));
        //}

        public static DataSet GetBrandsListDaynamic(string CategoryCode, string ManuCode, decimal domainid)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_SelectBrandDaynaic", CategoryCode, ManuCode, domainid);
        }

        public static DataSet GetAllBrandsListDaynamic(string CategoryCode, string ManuCode, decimal domainid,string valid)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_SelectAllBrandDaynaic", CategoryCode, ManuCode, domainid,valid);
        }

        public static ObjectTable GetVALIDYes(int Valid)
        {
            return new ObjectTable(typeof(BRANDWITHCATEGORY), string.Format("this.ACTIVE='{0}'", Valid));
        }

        public static ObjectTable GetBrands(decimal domainid)
        {
            return new ObjectTable(typeof(BRANDS), "this.DOMAINID='" + domainid + "' and this.ACTIVE='1'");
        }

        public static ObjectTable GetBrandwithCategory(string CategoryCode, string ManuCode, decimal domainID)
        {
            return new ObjectTable(typeof(BRANDS), string.Format("this.CategoryCode='{0}' and this.DOMAINID='{1}' and this.ManuCode='{2}' and this.ACTIVE='1'", CategoryCode, domainID, ManuCode));
            
        }

        //public static ObjectTable GetBrandwithManu(string ManuCode,decimal domainID)
        //{
        //    return new ObjectTable(typeof(BRANDS), string.Format("this.ManuCode='{0}' AND this.DOMAINID='{1}'", ManuCode,domainID));
        //}

        /// <summary>
        /// 保存品牌信息
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public bool Save(BRANDS brand)
        {
            try
            {
                if (this.Load(brand.BrandCode, brand.DOMAINID,brand.ID) != null)
                {
                    throw new Exception("代码已存在");
                }
               
                brand.Save();

                return true;
            }
            catch (Exception ex)
            {
                brand.Connection.RollbackTransaction();
                this.message = ex.Message;
                throw ex;
            }
        }
    }
}
